package net.csdn.business.common.domain;

import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;
import net.csdn.business.common.constant.CommonConstants;
import net.csdn.business.common.enums.ResultCodeEnum;
import net.csdn.business.common.i18n.LocaleMessageUtil;
import org.slf4j.MDC;

/**
 * @author likun
 * @date 2022/12/9 14:08
 */
@Data
@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ApiErrorResult {
    private String cause;
    @JSONField(name = "error_code")
    private String errorCode;
    // @JSONField(name = "error_name")
    // private String errorName;
    @JSONField(name = "error_message")
    private String errorMessage;
    @JSONField(name = "trace_id")
    private String traceId = MDC.get(CommonConstants.TRACE_ID);

    /******** 兼容结构 ***********/
    private Integer code;

    private String msg;

    public ApiErrorResult(String errorCodeName, int code, String errorMessage) {
        this.cause = String.format(LocaleMessageUtil.getEnMessage("MESSAGE." + errorCodeName), "");
        this.errorCode = errorCodeName;
        // this.errorName = errorCodeName;
        this.errorMessage = errorMessage;

        this.code = code;
        this.msg = errorMessage;
    }

    public ApiErrorResult(ResultCodeEnum resultCode) {
        String msg = String.format(LocaleMessageUtil.getMessage(resultCode), "");
        this.cause = LocaleMessageUtil.getEnMessage(resultCode.getI18n());
        this.errorCode = resultCode.name();
        // this.errorName = resultCode.name();
        this.errorMessage = msg;

        this.code = resultCode.getCode();
        this.msg = msg;
    }
}